package org.xue.snowmall.authserver.service.impl;

import entity.UserRecordEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.xue.snowmall.authserver.config.JwtUtil;
import org.xue.snowmall.authserver.vo.UserLoginVo;
import org.xue.snowmall.authserver.vo.UserRegisterVo;
import org.xue.snowmall.common.utils.PageUtils;
import org.xue.snowmall.common.utils.Query;

import org.xue.snowmall.authserver.dao.UserRecordDao;
import org.xue.snowmall.authserver.service.UserRecordService;

import javax.servlet.http.HttpSession;


@Service("userRecordService")
public class UserRecordServiceImpl extends ServiceImpl<UserRecordDao, UserRecordEntity> implements UserRecordService {

    @Autowired
    private HttpSession session;
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private BCryptPasswordEncoder encoder;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserRecordEntity> page = this.page(
                new Query<UserRecordEntity>().getPage(params),
                new QueryWrapper<UserRecordEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * @description: 登录接口
            * @return: java.lang.String
            * @author: 小刘的小张
            * @time: 2021/9/22 14:05
     */
    @Override
    public String login(UserLoginVo vo) {

        UserRecordEntity userRecordEntity = baseMapper.selectOne(new QueryWrapper<UserRecordEntity>().eq("usm_name", vo.getLoginacct()).or().eq("usm_mobile", vo.getLoginacct()));
        if (userRecordEntity != null) {
            if (encoder.matches(vo.getPassword(), userRecordEntity.getUsmPassword())) {
                //生成token
                String token = jwtUtil.createJWT(userRecordEntity.getUsmId() + "", userRecordEntity.getUsmName(), "role");
                userRecordEntity.setToken(token);
                session.setAttribute("user", userRecordEntity);
                return "redirect:http://"+vo.getUrl();
            }
        }

        return "redirect:http://auth.snow-mall.com/";
    }

    @Override
    public String register(UserRegisterVo user) {

        //从redis 中获取验证码
        String code = redisTemplate.opsForValue().get(user.getPhone());
        if (user.getCode().equals(code)) {
            String encode = encoder.encode(user.getPassword());
            user.setPassword(encode);
            //此时可以进行注册
            UserRecordEntity userRecordEntity = new UserRecordEntity();
            userRecordEntity.setUsmName(user.getUserName());
            userRecordEntity.setUsmPassword(user.getPassword());
            userRecordEntity.setUsmMobile(user.getPhone());
            baseMapper.insert(userRecordEntity);
            return "redirect:http://auth.snow-mall.com";
        }
        return "redirect:http://auth.snow-mall.com/register";
    }

    @Override
    public UserRecordEntity findByToken(String access_token) {

        return baseMapper.selectOne(new QueryWrapper<UserRecordEntity>().eq("usm_access_token",access_token));
    }

}